﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.video
{
      public partial class videolist_e : System.Web.UI.Page
      {
            private readonly D_T_Video_Exp m_dal = new D_T_Video_Exp();
            M_T_Video _bmodel = new M_T_Video();
            M_T_VideoCD _rmodel = new M_T_VideoCD();
            private readonly D_T_VideoCD m_video = new D_T_VideoCD();
            private readonly D_T_ExperInfot_Exp m_expert = new D_T_ExperInfot_Exp();
            private void SetButton(int i, Button but)
            {
                  switch (i)
                  {
                        case 1:
                              but.CausesValidation = false;
                              break;
                        default:
                              break;
                  }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        this.WebUserToolBar1.MainInit(
                              UserConst.Exp_GetUser,
                              UserConst.Exp_GetButtonList,
                              SetButton,
                             UserConst.Exp_ToolBarErr
                          );
                        if (!IsPostBack)
                        {
                              BindCateDrop();
                              BindExpert();
                              var id = Request.Params["ID"].Exp_IntTryParse();
                              ViewState.Add("id", id);
                              if (id > 0)
                              {
                                    var model = m_dal.GetModel(id);//获得视频实体
                                    if (model == null)
                                    {
                                          this.Exp_PageSkyAlterReplace("videolist.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                                    }
                                    else
                                    {
                                          SetControlsData(model);
                                    }
                              }
                        }
                  });
            }

            /// <summary>
            /// 编辑模式的时候,填充窗口
            /// </summary>
            /// <param name="model"></param>
            private void SetControlsData(M_T_Video model)
            {
                  AdminPublic.Try(() =>
                  {
                        this.HGdropCate.SelectedValue = model.CategoryID.Exp_Trim();
                        this.DropDownList1.SelectedValue = model.ExperInfotID.Exp_Trim();
                        this.txtVideoURL.Text = model.URL.Exp_Trim();
                        this.txtTitle.Text = model.Title.Exp_Trim();
                        
                        this.txtGrade.Text = model.Grade.Exp_Trim();
                        this.txtGold.Text = model.Gold.Exp_Trim();
                        this.txtSource.Text = model.Source.Exp_Trim();

                        //置顶为1，不置顶为0
                        this.rdoListTopis.Text = model.Topis.Exp_Trim();
                        //推荐为1，不推荐为0
                        this.rblRem.Text = model.RemSign ? "1" : "0";

                        this.rdoList.Text = model.HasVideoCD ? "1" : "0";
                        this.txtSEOtitle.Text = model.WebTitle.Exp_Trim();
                        this.txtSEOkey.Text = model.WebKey.Exp_Trim();
                        this.txtSEOdec.Text = model.WebSummary.Exp_Trim();
                        this.txtContent.Value = model.Explain.Exp_Trim();
                        this.FLWUpLoad1.SetIMG(model.Pic.Exp_Trim());

                        if (this.rdoList.Text == "1") //绑定CD
                        {
                              this.panCD.Visible = true;
                              var vCD = m_video.GetList("VideoID = " + model.id, null);
                              foreach (var v in vCD) 
                              {
                                    this.txtCDName.Text = v.CDName.Exp_Trim();
                                    this.txtRealISBN.Text = v.ISBN.Exp_Trim();
                                    this.txtRealPublish.Text = v.Publish.Exp_Trim();
                                    this.txtRealAuthor.Text = v.Author.Exp_Trim();
                                    this.txtRealPrice.Text = v.Price.Exp_Trim();
                                    this.txtRealGold.Text = v.Gold.Exp_Trim();
                                    this.txtStockCount.Text = v.StockNum.Exp_Trim();
                                    this.txtRealContent.Value = v.Explain.Exp_Trim();
                                    this.FLWUpLoad2.SetIMG(v.Pic.Exp_Trim());
                                    break;
                              }
                        }
                  });
            }

            /// <summary>
            /// 绑定下拉列表框
            /// </summary>
            private void BindCateDrop()
            {
                  AdminPublic.Try(() =>
                  {
                        this.HGdropCate.Exp_BindClassTree();
                  });
            }

            private void BindExpert() 
            {
                  AdminPublic.Try(() =>
                  {
                        var expertlist = m_expert.DropDownListDatData();
                        this.DropDownList1.DataValueField = "ID";
                        this.DropDownList1.DataTextField = "Name";
                        this.DropDownList1.DataSource = expertlist;
                        this.DropDownList1.DataBind();
                  });
            }

            protected void WebUserToolBar1_Button0(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        //注意,统一,先获取实体,然后根据实体进行各种判断
                        var model = ControlsToModel();
                        var realModel = GetRealbookModel();
                        var LogType = Log_Type.add;

                        var iRef = 0;
                        if (0 >= model.id)
                        {
                              var fileName = this.FLWUpLoad1.UpLoad();
                              if (!fileName)
                              {
                                    model.Pic = "";
                              }
                              else
                              {
                                    model.Pic = FLWUpLoad1.FilePath;
                              }

                              if (model.HasVideoCD) 
                              {
                                    var cdPic = this.FLWUpLoad2.UpLoad();
                                    if (!cdPic)
                                    {
                                          realModel.Pic = "";
                                    }
                                    else 
                                    {
                                          realModel.Pic = FLWUpLoad2.FilePath;
                                    }
                              }
                              iRef = m_dal.Add(model, this.rdoList.Text == "1", realModel);
                        }
                        else
                        {
                              //    修改的专用判断
                              LogType = Log_Type.update;
                              var Temp = m_dal.GetModelFlag(model.id);
                              if (Temp == null)
                              {
                                    this.Exp_PageAlter("资料不存在,可能被其他人删除");
                                    return;
                              }

                              var fileName = this.FLWUpLoad1.UpLoad();
                              if (!fileName)
                              {
                                    model.Pic = Temp.Pic;
                              }
                              else
                              {
                                    model.Pic = FLWUpLoad1.FilePath;
                              }

                              if (this.rdoList.Text == "1")
                              {
                                    var cdPic = this.FLWUpLoad2.UpLoad();
                                    if (!cdPic)
                                    {
                                           var vCD = m_video.GetList("VideoID = " + model.id, null);
                                          foreach(var v in vCD)
                                          {
                                                realModel.Pic = v.Pic;
                                                break;
                                          }
                                    }
                                    else
                                    {
                                          realModel.Pic = FLWUpLoad2.FilePath;
                                    }
                              }
                              else if (realModel.VideoID > 0)//删除实体书
                              {
                                    m_dal.DelCDFlag(realModel.VideoID);
                                    realModel = null;
                              }

                              iRef = m_dal.Update(model, this.rdoList.Text == "1", realModel);
                        }
                        EditSaveMsg(iRef, LogType);
                  });
            }

            /// <summary>
            /// 操作完成后,处理各种提示用
            /// </summary>
            /// <param name="iRef"></param>
            /// <param name="LogType"></param>
            public void EditSaveMsg(int iRef, Log_Type LogType)
            {
                  if (iRef > 0)
                  {
                        this.Exp_PageWriteLog(LogType, "视频资料维护", iRef);
                        this.Exp_PageSkyAlterReplace("videolist.aspx", WebUserToolBar1.EditSKey, "操作成功");
                  }
                  else
                  {
                        switch (iRef)
                        {
                              default:
                                    this.Exp_PageAlter("操作失败");
                                    break;
                        }
                  }
            }

            private M_T_Video ControlsToModel()
            {
                _bmodel.id = ViewState["id"].Exp_IntTryParse();             
                  _bmodel.Title = this.txtTitle.Text.Exp_Trim();
                  _bmodel.Source = this.txtSource.Text.Exp_Trim();
                  _bmodel.CategoryID = this.HGdropCate.SelectedValue.Exp_IntTryParse();
                  _bmodel.Type = "flv";
                  _bmodel.URL = this.txtVideoURL.Text.Exp_Trim();
                  _bmodel.Size = 1024;
                  _bmodel.Explain = this.txtContent.Value.Exp_Trim();
                  _bmodel.Gold = this.txtGold.Text.Exp_DecimalTryParse();
                  _bmodel.Grade = this.txtGrade.Text.Exp_IntTryParse();
                  _bmodel.HasVideoCD = this.rdoList.Text == "0" ? false : true;
                  _bmodel.WebKey = this.txtSEOkey.Text != "" ? this.txtSEOkey.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                  _bmodel.WebSummary = this.txtSEOdec.Text != "" ? this.txtSEOdec.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                  _bmodel.WebTitle = this.txtSEOtitle.Text != "" ? this.txtSEOtitle.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                  _bmodel.ColorKey = string.Empty;
                  _bmodel.ColorTitle = string.Empty;
                  _bmodel.InnerKey = string.Empty;
                  _bmodel.AddDate = DateTime.Now;

                  //置顶
                  _bmodel.Topis = this.rdoListTopis.Text.Exp_IntTryParse();
                  //推荐
                  _bmodel.RemSign = this.rblRem.Text == "0" ? false : true;

                  _bmodel.UserID = UserConst.Exp_GetUser(this).id;
                  _bmodel.ExperInfotID = this.DropDownList1.SelectedValue.Exp_IntTryParse();
                  return _bmodel;
            }

            private M_T_VideoCD GetRealbookModel()
            {
                  _rmodel.VideoID = ViewState["id"].Exp_IntTryParse();
                  _rmodel.CDName = this.txtTitle.Text.Exp_Trim();
                  _rmodel.Author = this.txtRealAuthor.Text.Exp_Trim();
                  _rmodel.ISBN = this.txtRealISBN.Text.Exp_Trim();
                  //图片
                  var tempModel = m_video.GetModel(_rmodel.id);
                  _rmodel.Explain = this.txtRealContent.Value.Exp_Trim();
                  _rmodel.Gold = this.txtRealGold.Text.Exp_DecimalTryParse();
                  _rmodel.Grade = 0;
                  _rmodel.WebKey = string.Empty;
                  _rmodel.WebSummary = string.Empty;
                  _rmodel.WebTitle = string.Empty;
                  _rmodel.ColorKey = string.Empty;
                  _rmodel.ColorTitle = string.Empty;
                  _rmodel.InnerKey = string.Empty;
                  _rmodel.SellNum = 0;
                  _rmodel.StockNum = this.txtStockCount.Text.Exp_IntTryParse();
                  _rmodel.Publish = this.txtRealPublish.Text.Exp_Trim();
                  return _rmodel;
            }

            protected void WebUserToolBar1_Button1(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        var sKey = this.WebUserToolBar1.EditSKey;
                        Response.Redirect("videolist.aspx?" + sKey);
                  });
            }

            protected void rdoList_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.panCD.Visible = this.rdoList.SelectedItem.Value == "1";
            }
      }
}
